/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.web.captcha.support;

import org.vacoor.nothing.web.captcha.ImageCaptcha;
import org.vacoor.nothing.web.captcha.ImageCaptchaDao;

import java.io.Serializable;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author vacoor
 */
public class MemoryImageCaptchaDao implements ImageCaptchaDao {
    private ConcurrentMap<Serializable, ImageCaptcha> captchaMap;

    public MemoryImageCaptchaDao() {
        captchaMap = new ConcurrentHashMap<Serializable, ImageCaptcha>();
    }

    @Override
    public ImageCaptcha read(Serializable id) {
        return captchaMap.get(id);
    }

    @Override
    public void store(Serializable id, ImageCaptcha captcha) {
        captchaMap.put(id, captcha);
    }

    @Override
    public void delete(Serializable id) {
        captchaMap.remove(id);
    }

    @Override
    public Collection<ImageCaptcha> getActiveCaptchas() {
        return captchaMap.values();
    }
}
